--- title: How does the US spending on kids change? author: Xuxin Zhang date: '2020-09-22' slug: post categories: - R project tags: - data visualization - gganimate subtitle: '' summary: '' authors: [] lastmod: '2020-09-22T16:33:37+08:00' featured: no image: caption: '' focal_point: '' preview_only: no projects: [] ---
library(tidyverse)
kids <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')
theme_set(theme_light())
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.0.2
top_states_pub<-kids%>%filter(variable=="pubhealth")%>%
filter(year ==2012)%>%
arrange(desc(inf_adj))%>%
head(3)%>%pull(state)
bottom_states_pub<-kids%>%filter(variable=="pubhealth")%>%
filter(year ==2012)%>%
arrange(inf_adj)%>%
head(3)%>%pull(state)
kids%>%filter(variable=="pubhealth")%>%
mutate(top_bottom = case_when(state%in%top_states_pub~"Top",
state%in%bottom_states_pub~"Bottom",
TRUE~"Others"),
state_plot =case_when(state%in%top_states_pub~state,
state%in%bottom_states_pub~state,
TRUE~""))%>%
ggplot(aes(x = year, y = inf_adj, group = state))+
geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
geom_text(aes(x = year, y = inf_adj,label = state_plot),
family = "Times",
check_overlap = FALSE)+
scale_size_manual(values = c(1.5,0.2,1.5))+
scale_color_manual(values = c("pink","grey","lightgreen"))+
scale_y_log10()+
labs(x = "Year",
y = "Inflation-adjusted spending on public health",
title = "How the spending on public health (in $1,000s) changes")+
theme(legend.position = "null",
plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
theme_classic()+
transition_reveal(year)

top_states_unemp<-kids%>%filter(variable=="unemp")%>%
filter(year ==2012)%>%
arrange(desc(inf_adj))%>%
head(3)%>%pull(state)
bottom_states_unemp<-kids%>%filter(variable=="unemp")%>%
filter(year ==2012)%>%
arrange(inf_adj)%>%
head(3)%>%pull(state)
kids%>%filter(variable=="unemp")%>%
mutate(top_bottom = case_when(state%in%top_states_unemp~"Top",
state%in%bottom_states_unemp~"Bottom",
TRUE~"Others"),
state_plot =case_when(state%in%top_states_unemp~state,
state%in%bottom_states_unemp~state,
TRUE~""))%>%
ggplot(aes(x = year, y = inf_adj,group = state))+
geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
geom_text(aes(x = year, y = inf_adj,label = state_plot),
family = "Times",
check_overlap = FALSE)+
scale_size_manual(values = c(1.5,0.2,1.5))+
scale_color_manual(values = c("pink","grey","lightgreen"))+
scale_y_log10()+
labs(x = "Year",
y = "Inflation-adjusted spending on unemployment benefits",
title = "How the Unemployment benefits (in $1,000s) changes")+
theme(legend.position = "null",
plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
theme_classic()+
transition_reveal(year)

top_states_pk<-kids%>%filter(variable=="PK12ed")%>%
filter(year ==2012)%>%
arrange(desc(inf_adj))%>%
head(3)%>%pull(state)
bottom_states_pk<-kids%>%filter(variable=="PK12ed")%>%
filter(year ==2012)%>%
arrange(inf_adj)%>%
head(3)%>%pull(state)
kids%>%filter(variable=="pubhealth")%>%
mutate(top_bottom = case_when(state%in%top_states_pk~"Top",
state%in%bottom_states_pk~"Bottom",
TRUE~"Others"),
state_plot =case_when(state%in%top_states_pk~state,
state%in%bottom_states_pk~state,
TRUE~""))%>%
ggplot(aes(x = year, y = inf_adj, group = state))+
geom_point(aes(size = top_bottom,color = top_bottom), alpha = 0.5,show.legend = FALSE)+
geom_path(aes(size = top_bottom,color = top_bottom),show.legend = FALSE)+
geom_text(aes(x = year, y = inf_adj,label = state_plot),
family = "Times",
check_overlap = FALSE)+
scale_size_manual(values = c(1.5,0.2,1.5))+
scale_color_manual(values = c("pink","grey","lightgreen"))+
scale_y_log10()+
labs(x = "Year",
y = "Inflation-adjusted spending on K12 education",
title = "How the spending on K12 education (in $1,000s) changes")+
theme(legend.position = "null",
plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
theme_classic()+
transition_reveal(year)

library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
state_year_loc <- kids%>%filter(variable=="PK12ed")%>%
mutate(year2 = as.factor(year))%>%
mutate(state = str_to_lower(state))%>%
group_by(state, year2)%>%summarise(inf_adj= mean(inf_adj))%>%
right_join(map_data("state"),by =c("state"="region"))
## `summarise()` regrouping output by 'state' (override with `.groups` argument)
ggplotly(state_year_loc%>%mutate(year = as.numeric(year2)) %>%
ggplot(aes(x = long, y = lat, group = group))+
geom_polygon(aes(fill = inf_adj))+
scale_fill_gradient(low = "yellow", high = "orangered")+
facet_wrap(~year2)+
theme(plot.title = element_text(face = "bold", size = 14, margin = margin(20,0,5,0)))+
theme_void()+
labs(title = "The heatmap of spending on K12 education from 1997 to 2016",
fill = "Spending"))